package cn.edu.djtu.fw.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.djtu.fw.model.News;
import cn.edu.djtu.fw.model.Json;
import cn.edu.djtu.fw.service.NewsServiceI;
import cn.edu.djtu.fw.util.JsonUtil;

import com.opensymphony.xwork2.ActionSupport;

public class NewsAction extends ActionSupport implements ServletRequestAware {
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private NewsServiceI newsService;
	private News news;
	private int page;
	private int rows;
	private String ids;

	@Override
	public String execute() throws Exception {
		Map<String, Object> m = new HashMap<String, Object>();
		m = newsService.getAllNews(page, rows, news);
		JsonUtil.writeJson(m);
		return null;
	}
	public String show() throws Exception {
		HttpSession session = request.getSession();
		News news= new News();
		news=newsService.getNewsById(nid);
		if (news!=null) {
			session.setAttribute("currentNews", news);
			int count = news.getHitcount();
			count=count+1;
			news.setHitcount(count);
			newsService.update(news);
		}
		return "news";
	}
	public void add() throws Exception {
		Json j = new Json();
		if (newsService.insert(news)) {
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} else {
			j.setMsg("添加失败！");
		}
		JsonUtil.writeJson(j);
	}

	public void update() throws Exception {
		Json j = new Json();
		if (newsService.update(news)) {
			j.setSuccess(true);
			j.setMsg("修改成功！");
		} else {
			j.setMsg("修改失败！");
		}
		JsonUtil.writeJson(j);
	}

	public void delete() throws Exception {
		Json j = new Json();
		String[] idsStr = ids.split(",");
		for (int i = 0; i < idsStr.length; i++) {
			News c = newsService.getNewsById(Integer.parseInt(idsStr[i]));
			if (c != null) {
				newsService.delete(c.getNid());
			}
		}
		j.setSuccess(true);
		j.setMsg("删除成功！");
		JsonUtil.writeJson(j);
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public News getNews() {
		return news;
	}

	public void setNews(News news) {
		this.news = news;
	}

	public NewsServiceI getNewsService() {
		return newsService;
	}

	@Autowired
	public void setNewsService(NewsServiceI newsService) {
		this.newsService = newsService;
	}

	@Override
	public void setServletRequest(HttpServletRequest req) {
		this.request = req;
	}
	private Integer nid;

	public Integer getNid() {
		return nid;
	}
	public void setNid(Integer nid) {
		this.nid = nid;
	}
	

}
